package com.itheima.jdbc;
import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
public class TeacherDaoImpl implements TeacherDao {
	// 声明JdbcTemplate属性及其setter方法
	private TeacherDaoImpl teacherDaoImpl;
	public void setTeacherDaoImpl(TeacherDaoImpl teacherDaoImpl) {
		this.teacherDaoImpl = teacherDaoImpl;
	}
    // 添加账户
	public int addTeacher(Teacher teacher) {
		// 定义SQL
		String sql = "insert into teacher(tid,tname,tage,tphone) value(?,?,?,?)";
		// 定义数组来存放SQL语句中的参数
		Object[] obj = new Object[] { 
                           teacher.getTid(), 
                           teacher.getTname(),
                           teacher.getTage(),
                           teacher.getTphone()
         };
		// 执行添加操作，返回的是受SQL语句影响的记录条数
		int num = this.teacherDaoImpl.update(sql, obj);
		return num;
	}
	// 更新账户
	public int updateTeacher(Teacher teacher) {
		// 定义SQL
		String sql = "update teacher set username=?,balance=? where id = ?";
		// 定义数组来存放SQL语句中的参数
		Object[] params = new Object[] { 
                               teacher.getUsername(), 
                               teacher.getBalance(), 
                               teacher.getId() 
          };
		// 执行更新操作，返回的是受SQL语句影响的记录条数
		int num = this.teacherDaoImpl.update(sql, params);
		return num;
	}
	// 删除账户
	public int deleteTeacher(int id) {
		// 定义SQL
		String sql = "delete  from teacher where id = ? ";
		// 执行删除操作，返回的是受SQL语句影响的记录条数
		int num = this.teacherDaoImpl.update(sql, id);
		return num;
	}
	
	// 通过id查询账户数据信息
	public Teacher findTeacherById(int id) {
	    //定义SQL语句
	    String sql = "select * from teacher where id = ?";
	    // 创建一个新的BeanPropertyRowMapper对象
	    RowMapper<Teacher> rowMapper = 
	new BeanPropertyRowMapper<Teacher>(Teacher.class);
	    // 将id绑定到SQL语句中，并通过RowMapper返回一个Object类型的单行记录
	    return this.teacherDaoImpl.queryForObject(sql, rowMapper, id);
	}
	// 查询所有账户信息
	public List<Teacher> findAllTeacher() {
	    // 定义SQL语句
	    String sql = "select * from teacher";
	    // 创建一个新的BeanPropertyRowMapper对象
	    RowMapper<Teacher> rowMapper = 
	new BeanPropertyRowMapper<Teacher>(Teacher.class);
	    // 执行静态的SQL查询，并通过RowMapper返回结果
	    return this.teacherDaoImpl.query(sql, rowMapper);
	}

}
